<?php
/**
 * Copyright (c) 2016.  hangzhou BOYE .Co.Ltd. All rights reserved
 */

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-12-17
 * Time: 16:22
 */

namespace app\admin\controller;


use app\src\stores\logic\StoresLogic;
use app\src\stores\logic\StoresOrderLogic;
use app\src\tool\helper\GeoHashHelper;

use think\Log;

class Stores extends Admin
{


    /**
     * 店铺管理
     */
    public function index()
    {

        //分页时带参数get参数


        $name = $this->_param('name', '');
        $province = $this->_param('province', '');
        $city = $this->_param('city', '');
        $area = $this->_param('area', '');
        $id = $this->_param('id', 0);
        $leave = $this->_param('leave', 0);


        $map = array();
        if (!empty($name)) {
            $map['name'] = array('like', "%" . $name . "%");
        }
        if (!empty($province)) {
            $map['province'] = $province;
        }
        if (!empty($city)) {
            $map['city'] = $city;
        }
        if (!empty($area)) {
            $map['area'] = $area;
        }

        $map['father'] = $id;


        $page = array('curpage' => $this->_param('p', 0), 'size' => config('LIST_ROWS'));
        $order = " create_time desc ";

        $result = (new StoresLogic())->queryWithPagingHtml($map, $page, $order);

        if ($result['status']) {
            $this->assign('id', $id);
            $this->assign('name', $name);
            $this->assign('leave', $leave);
            $this->assign('show', $result['info']['show']);
            $this->assign('list', $result['info']['list']);
            return $this->boye_display();
        } else {
            Log::record('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
    }

    public function add()
    {
        if (IS_GET) {
            $id = $this->_param('id', 0);
            $leave = $this->_param('leave', 0);
            $this->assign('id', $id);
            $this->assign('leave', $leave);
            return $this->boye_display();
        } elseif (IS_POST) {
            $name = $this->_param('name', '门店名称');//
            $logo = $this->_param('logo', '');


            $lat = $this->_param('lat', '');
            $lng = $this->_param('lng', '');
            $phone = $this->_param('phone', '');
            $father = $this->_param('father', 0);
            $addres = $this->_param('addres', '');
            $leave = $this->_param('leave', 1) + 1;
            $province = $this->_param('province', '');
            $city = $this->_param('city', '');
            $area = $this->_param('area', '');
            $desc = $this->_param('desc', '');
            $open_time = $this->_param('open_time', '');
            $geohash = (new GeoHashHelper())->encode($lat, $lng);

            $entity = array(
                'lat' => $lat,
                'lng' => $lng,
                'leave' => $leave,
                'addres' => $addres,
                'uid' => UID,
                'name' => $name,
                'logo' => $logo,
                'phone' => $phone,
                'father' => $father,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'geohash' => $geohash,
                'desc' => $desc,
                'open_time' => $open_time
            );

            $result = (new StoresLogic())->add($entity);
            if ($result['status']) {
                $this->success("操作成功！", url('Admin/Stores/index'));
            } else {
                $this->error($result['info']);
            }
        }
    }


    public function edit()
    {
        if (IS_GET) {
            $id = $this->_param('id', 0);
            $map = array('id' => $id);
            $result = (new StoresLogic())->getInfo($map);
            if ($result['status']) {
                $this->assign("stores", $result['info']);
                return $this->boye_display();
            } else {
                $this->error($result['info']);
            }
        } elseif (IS_POST) {
            $id = $this->_param('id', 0);
            $name = $this->_param('name', '门店名称');
            $logo = $this->_param('logo', '');
            $lat = $this->_param('lat', '');
            $lng = $this->_param('lng', '');
            $phone = $this->_param('phone', '');
            $addres = $this->_param('addres', '');
            $province = $this->_param('province', '');
            $city = $this->_param('city', '');
            $area = $this->_param('area', '');
            $desc = $this->_param('desc', '');
            $open_time = $this->_param('open_time', '');
            $geohash = (new GeoHashHelper())->encode($lat, $lng);
            $entity = array(
                'lat' => $lat,
                'lng' => $lng,
                'addres' => $addres,
                'uid' => UID,
                'name' => $name,
                'logo' => $logo,
                'phone' => $phone,
                'geohash' => $geohash,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'desc' => $desc,
                'open_time' => $open_time
            );
            $result = (new StoresLogic())->saveByID($id, $entity);

            if ($result['status']) {
                //暂时注释这个功能 by hebidu @20170103
//                $this->savePromotion($id);
                $this->success("操作成功！", url('Admin/Stores/index'));
            } else {
                $this->error($result['info']);
            }
        }
    }


    public function orderin()
    {
        if (IS_POST) {
            $order_id = $this->_param('id', 0);
            $lat = $this->_param('lat', 0);
            $lng = $this->_param('lng', 0);
            $area = $this->_param('area', 0);
            $geohash = (new GeoHashHelper())->encode($lat, $lng);
            $geohash = substr($geohash, 0, 5);
            $map['geohash'] = array('like', $geohash . "%");
            $map['leave'] = 4;
            $result = (new StoresLogic())->getInfo($map);
            if ($result['status']) {
                $entity = array(
                    'order_id' => $order_id,
                    'stores_id' => $result['info']['id']
                );
                $result = (new StoresOrderLogic())->add($entity);
                if ($result['status']) {
                    $this->success("操作成功！");
                } else {
                    $this->error($result['info']);
                }
            } else {
                $map['area'] = $area;
                $map['leave'] = 3;
                $result = (new StoresLogic())->getInfo($map);
                if ($result['status']) {
                    $entity = array(
                        'order_id' => $order_id,
                        'stores_id' => $result['info']['id']
                    );
                    $result = (new StoresOrderLogic())->add($entity);
                    if ($result['status']) {
                        $this->success("操作成功！");
                    } else {
                        $this->error($result['info']);
                    }
                }
            }
        }

    }

}